package com.dmooo.yxmmerchant.view.presenter;

import android.content.Context;

import com.common.mvpbase.BasePresenter;
import com.common.net.listener.HttpOnNextListener;
import com.common.util.LogUtil;
import com.dmooo.yxmmerchant.domain.ServiceDetailBean;
import com.dmooo.yxmmerchant.domain.ServiceListBean;
import com.dmooo.yxmmerchant.view.contract.ServiceContract;
import com.dmooo.yxmmerchant.view.model.ServiceModel;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import org.json.JSONException;
import org.json.JSONObject;

public class ServicePrestener extends BasePresenter<ServiceContract.ServiceView> implements ServiceContract.ServicePtr {
    private ServiceModel model;
    private GsonBuilder builder=new GsonBuilder();
    public ServicePrestener(ServiceContract.ServiceView mView, Context context) {
        super(mView);
        model=new ServiceModel(context);
        builder.serializeNulls();
    }
    @Override
    public void getServiceList(String cat_id, int page) {
        model.getServiceList(cat_id, page, new HttpOnNextListener() {
            @Override
            public void onNext(Object o) {
                LogUtil.d("dsafds",o.toString());
                try {
                    mView.showServiceList(new Gson().fromJson(new JSONObject(builder.create().toJson(o)).toString(),ServiceListBean.class).list);
                } catch (JSONException e) {
                    e.printStackTrace();
                    mView.errorMsg("数据解析错误");
                }
            }

            @Override
            public void onFail(Object o) {
                mView.errorMsg(o.toString());
            }

            @Override
            public void onStart() {

            }

            @Override
            public void onComplete() {

            }
        });
    }

    @Override
    public void getServiceDetail(String service_id) {
        model.getServiceDetail(service_id, new HttpOnNextListener() {
            @Override
            public void onNext(Object o) {
                LogUtil.d("dsafds",o.toString());
                try {
                    mView.showServiceDetail(new Gson().fromJson(new JSONObject(builder.create().toJson(o)).toString(),ServiceDetailBean.class));
                } catch (JSONException e) {
                    e.printStackTrace();
                    mView.errorMsg("数据解析错误");
                }
            }

            @Override
            public void onFail(Object o) {
                mView.errorMsg(o.toString());
            }

            @Override
            public void onStart() {

            }

            @Override
            public void onComplete() {

            }
        });
    }
}
